<?php
/**
*	前台用户
*/
class User extends CI_Controller{
	public function __construct(){
		parent::__construct();
		$this->load->model(array('user_model'));
		
	}
	
	//获得用户
	public function user(){}
	
	//添加用户
	public function add(){
		$this->load->helper('form');
		
		$this->load->library("form_validation");
		
		$rules = array(
			array(
				'field' => 'username',
				'label' => '用户名',
				'rules' => 'trim|required|callback_username_check'
			),				
			array(
				'field' => 'password',
				'label' => '密码',
				'rules' => 'trim|required'
			),				
			array(
				'field' => 'repassword',
				'label' => '重复密码',
				'rules' => 'trim|required|matches[password]|md5'
			),				
			array(
				'field' => 'email',
				'label' => '邮箱',
				'rules' => 'trim|required|valid_email'
			)
			
		);
		$this->form_validation->set_rules($rules);
		
		
		if($this->form_validation->run() === true){
			$this->load->helper('encrypt');
			$data = array(
				'username' => $this->input->post('username'),
				'password' => encryptUserPassword($this->input->post('password')),
				'email' => $this->input->post('email'),
				'role_id' => $this->input->post('role_id'),
				'status' => $this->input->post('status')
			);
			
			//添加成功
			if($this->user_model->addUser($data)){
				$this->lists();
				
			//添加失败
			}else{
			
			
				$this->load->view('templates/header');
				$this->load->view('admin/user/add');
				$this->load->view('templates/footer');
			}
			
		}else{
			$this->load->view('templates/header');
			$this->load->view('admin/user/add');
			$this->load->view('templates/footer');
		}
		

	}
	
	//编辑用户
	public function edit(){
		 
		$this->load->helper('form');
		$this->load->library("form_validation");
		$id = (int)$this->uri->segment(3);//取得id
		
	
		
		$rules = array(
			array(
				'field' => 'username',
				'label' => '用户名',
				'rules' => 'trim|required'
			),							
			array(
				'field' => 'repassword',
				'label' => '重复密码',
				'rules' => 'trim|matches[password]|md5'
			),				
			array(
				'field' => 'email',
				'label' => '邮箱',
				'rules' => 'trim|required|valid_email'
			)
			
		);
		$this->form_validation->set_rules($rules);
		
		
		if($this->form_validation->run() === true){
		
			$password = $this->input->post('password');
			
			$this->load->helper('encrypt');
			$data = array(
				'username' => $this->input->post('username'),
				'email' => $this->input->post('email'),
				'role_id' => $this->input->post('role_id'),
				'status' => $this->input->post('status')
			);
			
			if($password != ""){
				$data['password'] = encryptUserPassword($password);
			}
			
			//修改成功
			if($this->user_model->editUser($data,$this->input->post('user_id'))){
				$this->lists();
				
			//修改失败
			}else{
			}
			
		}else{
		
				$data = array(
					'userinfo' => $userinfo = $this->user_model->userInfo(array('id'=>$id))
				);
				
				$query = $this->db->get('admin_role');
				$data['roles'] =$query->result_array();
					
				$this->load->view('templates/header');
				$this->load->view('admin/user/edit',$data);
				$this->load->view('templates/footer');
		}
		
	}
	
	//删除用户
	public function delete(){
		if($this->user_model->deleteUser($this->input->post('id'))){
			$json['error'] = 0;
			$json['content'] = 0;
		}else{
			$json['error'] = 1;
			$json['content'] = '删除失败!';
		}
		echo json_encode($json);
		return ;
	}
	
	//用户列表
	public function lists(){
		$this->load->library('pagination');
		$config['base_url'] = site_url('user/lists');
		$config['total_rows'] = $this->db->count_all('admin_user');
		$config['per_page'] = 10;
		$this->pagination->initialize($config);
		$page = (int)$this->uri->segment(3);
		
		$data = array(
			'users' => $this->user_model->userList($config['per_page'],$page)
		);
		
		$this->load->view('templates/header');
		$this->load->view('admin/user/list',$data);
		$this->load->view('templates/footer');
	}
	
	//更新用户状态
	public function status(){
		$id = $this->input->post('id');
		$where = array(
			'status' => $this->input->post('status')
		);
		if($this->user_model->up_status($id,$where)){
		
			$json['error'] = 0;
			$json['content'] = 0;
			
		}else{
			$json['error'] = 1;
			$json['content'] = '更新失败!';
		}
		echo json_encode($json);
	    return ;
	} 
	
	//验证用户存在
	public function username_check($username){
		if($this->user_model->userInfo(array('username'=>$username))){
			$this->form_validation->set_message('username_check', 'The %s field can not be the word');
			return false;
		}else{
			return true;
		}
	}
	
	//uc中心概况
	public function ucconnect(){
		$data = array(
			'users' => $this->user_model->userCount()
		);
		$this->load->view('templates/header');
		$this->load->view('admin/user/ucconnect',$data);
		$this->load->view('templates/footer');
		
	}
	
	//用户同步到ucenter
	public function user2uc(){
		$this->user_model->user2uc();
	}
	
	//设置表单回调数据
	public function value_check($n){
		$this->form_validation->set_value($n);
	}
}
?>